import { _decorator, Node, tween, UIOpacity, v3, Vec3, view } from 'cc';
import { DEBUG } from 'cc/env';
import BlockMask from 'db://cocos-ext/base/BlockMask';
import { ComponentExt } from 'db://cocos-ext/component/ComponentExt';
import { games, lobby, yy } from 'yycore';

const { ccclass, property } = _decorator;

@ccclass('yy.Popup')
export class YYPopup extends ComponentExt {

    protected uiType: games.ui.UIType = 0;

    /** 窗口打开的时间戳 */
    protected uiTime: number = 0;

    // UI根节点
    protected get root(): Node {
        return this.node.getChildByName('root');
    }

    // UI最终缩放倍率
    protected get targetScale(): Vec3 {
        const size = view.getVisibleSize();
        const design = view.getDesignResolutionSize();
        const scaleX = size.width / design.width;
        const scaleY = size.height / design.height;
        let scale = this.root.scale.clone();
        if (scaleX <= scaleY) {
            scale = v3(scale.x * scaleX, scale.y * scaleX, scale.z * scaleX);
        } else if (scaleX > scaleY) {
            scale = v3(scale.x * scaleY, scale.y * scaleY, scale.z * scaleY);
        }
        return scale;
    }

    onEnable() {
        DEBUG && console.debug('# ui popup on enable ...');

        let e = new lobby.events.CustomEvent
        e.eventID = 'UI_YYPOPUP_SHOW'
        yy.event.emit(lobby.events.CustomEvent, e);

        let root = this.root;
        if (root) {
            BlockMask.addEventBlock();

            let targetScale = this.targetScale;
            tween(root)
                .set({ scale: Vec3.ZERO })
                .to(0.4, { scale: targetScale }, { easing: 'backOut' })
                .call(() => {
                    BlockMask.delEventBlock();
                })
                .start();

            let a = root.getComponent(UIOpacity);
            if (a) {
                tween(a)
                    .set({ opacity: 0 })
                    .to(0.2, { opacity: 255 })
                    .start();
            }

            let shadow = this.node.getChildByName('mask');
            if (shadow) {
                let a = shadow.getComponent(UIOpacity);
                if (a) {
                    tween(a)
                        .set({ opacity: 0 })
                        .to(0.1, { opacity: 255 })
                        .start();
                }
            }
        }

        this.uiTime = Date.now();
    }

    onDisable() {
        this.uiTime = 0;
    }

    onClose() {
        DEBUG && console.debug('# ui popup on close ...');

        let e = new lobby.events.CustomEvent
        e.eventID = 'UI_YYPOPUP_CLOSED'
        yy.event.emit(lobby.events.CustomEvent, e);

        let root = this.root;
        if (root) {
            BlockMask.addEventBlock();

            tween(root)
                .to(0.2, { scale: Vec3.ZERO }, { easing: 'backIn' })
                .call(() => {
                    this.onCloseEnd();
                    if (this.uiType > 0)
                        games.ui.manager.hidePublicUI(this.uiType);
                    else
                        this.visible = false;

                    if (root && root.isValid)
                        root.setScale(1, 1, 1);
                    BlockMask.delEventBlock();
                })
                .start();

            let a = root.getComponent(UIOpacity);
            if (a) {
                tween(a)
                    .to(0.2, { opacity: 0 })
                    .call(() => {
                        if (a && a.isValid)
                            a.opacity = 255;
                    })
                    .start();
            }
        }
        else
            this.onCloseEnd();
    }

    /**
     * 用于UI关闭后事件回调
     * 无实际内容，可以被重载
     */
    protected onCloseEnd() {
    }

}

